/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import java.util.ArrayList;
import java.util.HashSet;

import org.openaecfm.model.datetime.RecurrencePattern;
import org.openaecfm.model.datetime.RecurrenceTypeEnum;
import org.openaecfm.model.datetime.TimePeriod;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RecurrencePatternIO extends
		AbstractEntityInstanceInputImpl<RecurrencePattern> implements
		IEntityInstanceOutput<RecurrencePattern> {

	@Override
	public String exportSPF(RecurrencePattern pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCRECURRENCEPATTERN", appDataManager.getName(pojo));

		// Set RecurrenceType
		ei.addParameter(new EnumerationParameter(pojo.getRecurrenceType()));

		// Set DayComponent
		if (pojo.getDayComponent() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Integer i : pojo.getDayComponent()) {
				l.add(new IntegerParameter(i));
			}
			ei.addParameter(l);
		}

		// Set WeekdayComponent
		if (pojo.getWeekdayComponent() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Integer i : pojo.getWeekdayComponent()) {
				l.add(new IntegerParameter(i));
			}
			ei.addParameter(l);
		}

		// Set MonthComponent
		if (pojo.getMonthComponent() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Integer i : pojo.getMonthComponent()) {
				l.add(new IntegerParameter(i));
			}
			ei.addParameter(l);
		}

		// Set Position
		if (pojo.getPosition() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new IntegerParameter(pojo.getPosition()));
		}

		// Set Interval
		if (pojo.getInterval() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new IntegerParameter(pojo.getInterval()));
		}

		// Set Occurrences
		if (pojo.getOccurrences() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new IntegerParameter(pojo.getOccurrences()));
		}

		// Set TimePeriods
		if (pojo.getTimePeriods() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (TimePeriod t : pojo.getTimePeriods()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(t)));
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			RecurrencePattern pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TimePeriods
		Parameter timePeriods = sei.getParameter(7);
		if (timePeriods.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) timePeriods;
			if (pojo.getTimePeriods() == null) {
				pojo.setTimePeriods(new ArrayList<TimePeriod>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getTimePeriods().add(
						(TimePeriod) appDataManager.getObject(e
								.toInstanceName()));
			}
		}
	}

	@Override
	protected RecurrencePattern doCreateObject() {
		return new RecurrencePattern();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			RecurrencePattern pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RecurrenceType
		EnumerationParameter recurrenceType = (EnumerationParameter) sei
				.getParameter(0);
		pojo.setRecurrenceType(RecurrenceTypeEnum.valueOf(recurrenceType
				.toEnumString()));

		// Set DayComponent
		Parameter dayComponect = sei.getParameter(1);
		if (dayComponect.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) dayComponect;
			if (pojo.getDayComponent() == null) {
				pojo.setDayComponent(new HashSet<Integer>());
			}
			for (Parameter p : l) {
				IntegerParameter i = (IntegerParameter) p;
				pojo.getDayComponent().add(i.toInteger());
			}
		}

		// Set WeekdayComponent
		Parameter weekdayComponent = sei.getParameter(2);
		if (weekdayComponent.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) weekdayComponent;
			if (pojo.getWeekdayComponent() == null) {
				pojo.setWeekdayComponent(new HashSet<Integer>());
			}
			for (Parameter p : l) {
				IntegerParameter i = (IntegerParameter) p;
				pojo.getWeekdayComponent().add(i.toInteger());
			}
		}

		// Set MonthComponent
		Parameter monthComponent = sei.getParameter(3);
		if (monthComponent.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) monthComponent;
			if (pojo.getMonthComponent() == null) {
				pojo.setMonthComponent(new HashSet<Integer>());
			}
			for (Parameter p : l) {
				IntegerParameter i = (IntegerParameter) p;
				pojo.getMonthComponent().add(i.toInteger());
			}
		}

		// Set Position
		Parameter position = sei.getParameter(4);
		if (position.getNodeType() == NodeType.INTEGER) {
			IntegerParameter i = (IntegerParameter) position;
			pojo.setPosition(i.toInteger());
		}

		// Set Interval
		Parameter interval = sei.getParameter(5);
		if (interval.getNodeType() == NodeType.INTEGER) {
			IntegerParameter i = (IntegerParameter) interval;
			pojo.setInterval(i.toInteger());
		}

		// Set Occurrences
		Parameter occurrences = sei.getParameter(6);
		if (occurrences.getNodeType() == NodeType.INTEGER) {
			IntegerParameter i = (IntegerParameter) occurrences;
			pojo.setOccurrences(i.toInteger());
		}
	}
}
